//
//  AlertViewOneBtn.m
//  u76ho
//
//  Created by m-air-01 on 16/1/7.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "AlertViewOneBtn.h"
@interface AlertViewOneBtn ()
{
    NSString * title;
    NSString * content;
    NSString * btnName;
    
    UIImageView * _imageView;
}
@end
@implementation AlertViewOneBtn
-(instancetype)initWithFrame:(CGRect)frame title:(NSString *)text content:(NSString *)contentStr btnName:(NSString *)BtnStr
{
    self = [super initWithFrame:frame];
    if (self) {
        title = text;
        content = contentStr;
        btnName = BtnStr;
        [self addScreenShot];
        [self addAlertView];
    }
    return self;
}
-(void)addScreenShot{
    _imageView = [[UIImageView alloc]initWithFrame:self.bounds];
    UIImage * image = [UIImage imageScreenView:RootWindow];
    _imageView.image = image.blurImage();
    [self addSubview:_imageView];
}
-(void)addAlertView
{
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(SIZETO6W(23), SCREEN_HEIGHT/2-200/2, SCREEN_WIDTH-SIZETO6W(23)*2, 200)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.userInteractionEnabled = YES;
    [self addSubview:bgView];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, SIZETO6H(23), bgView.width, 20)];
    titleLab.text = title;
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = UIColorFromRGB(0x333944);
    [bgView addSubview:titleLab];
    
    UILabel * contentLab = [[UILabel alloc]initWithFrame:CGRectMake(0, titleLab.bottom+SIZETO6H(30), bgView.width, 20)];
    contentLab.text = content;
    contentLab.textColor = UIColorFromRGB(0x616978);
    contentLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:contentLab];
    
    CGFloat btnWidth = bgView.width - SIZETO6W(32)*2;
    
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(SIZETO6W(32), bgView.height-SIZETO6H(25) - 41, btnWidth, 41);
    [btn setBackgroundColor:UIColorFromRGB(0x6ec844)];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:btnName forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 20;
    [btn setTitleColor:UIColorFromRGB(0xFFFFFF) forState:UIControlStateNormal];
    [bgView addSubview:btn];
    
    UIImageView * closeImg = [[UIImageView alloc]initWithFrame:CGRectMake(bgView.width-SIZETO6W(19)-SIZETO6W(35), 0, SIZETO6W(35), 40)];
    closeImg.userInteractionEnabled = YES;
    closeImg.image = [UIImage imageNamed:@"close.png"];
    [bgView addSubview:closeImg];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [closeImg addGestureRecognizer:tap];
    
    [self animation:bgView addSize:YES];
}
-(void)btnAction:(UIButton *)btn
{
    [self removeFromSuperview];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"oneBtnSected" object:nil];
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    [self removrFromeSuperViewers];
}
-(void)animation:(UIView *)view addSize:(BOOL)is
{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.2f;
    animation.repeatCount = 1;
    
    if (is) {
        animation.autoreverses = NO;
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        [view.layer addAnimation:animation forKey:@"alertyes"];
    }else{
        animation.autoreverses = YES;
        animation.fromValue = [NSNumber numberWithFloat:1.0f];
        animation.toValue = [NSNumber numberWithFloat:0.0f];
        [view.layer addAnimation:animation forKey:@"alertno"];
    }
}
-(void)removrFromeSuperViewers
{
    _imageView.hidden = YES;
    [self animation:self addSize:NO];
    
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
